package com.dengyafei.service.impl;


import com.dengyafei.bean.AppointmentEntity;
import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.bean.GraduateEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.domin.bo.AppointmentSendBO;
import com.dengyafei.domin.dto.AppointmentAddDTO;
import com.dengyafei.domin.dto.AppointmentCancelDTO;
import com.dengyafei.domin.vo.AppointmentAddVO;
import com.dengyafei.domin.vo.AppointmentCancelVO;
import com.dengyafei.domin.vo.AppointmentSearchVO;
import com.dengyafei.domin.vo.AppointmentSendVO;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.AppointmentEntityMapper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.mapper.GraduateEntityMapper;
import com.dengyafei.service.AppointmentService;
import com.dengyafei.utils.DateUtil;
import com.dengyafei.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author 邓亚非
 */
@Slf4j
@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentEntityMapper appointmentEntityMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    GraduateEntityMapper graduateEntityMapper;
    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;
    @Autowired
    JavaMailSender javaMailSender;
    /**
     * 添加一个预约记录
     *
     * @param appointmentAddVO
     * @return
     */
    @Override
    public Integer insertOne(AppointmentAddVO appointmentAddVO) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        GraduateEntity graduateEntity = graduateEntityMapper.queryAllByGname(userInfo);
        AppointmentEntity appointmentEntity1 = appointmentEntityMapper.checkIsAppointment(appointmentAddVO.getMid(), graduateEntity.getGid());
        if (!ObjectUtils.isEmpty(appointmentEntity1)) {
            return RecommendConstant.ALREADY_APPOINTMENT;
        }
        appointmentAddVO.setGid(graduateEntity.getGid());
        appointmentAddVO.setGname(graduateEntity.getGname());
        log.info(appointmentAddVO.toString());
        AppointmentAddDTO appointmentAddDTO = BeanHelper.copyProperties(appointmentAddVO, AppointmentAddDTO.class);
        AppointmentEntity appointmentEntity = BeanHelper.copyProperties(appointmentAddDTO, AppointmentEntity.class);
        log.info(appointmentEntity.toString());
        return appointmentEntityMapper.insertSelective(appointmentEntity);
    }

    /**
     * 查询宣讲会
     * @param
     * @return
     */
    @Override
    public List<AppointmentEntity> queryByHid() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        return appointmentEntityMapper.queryAllApps(enterpriseEntity.getHid());
    }

    /**
     * 企业用户查询预约
     * @param appointmentSearchVO
     * @return
     */
    @Override
    public List<AppointmentEntity> queryByHidAndParams(AppointmentSearchVO appointmentSearchVO) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        return appointmentEntityMapper.queryByParams(enterpriseEntity.getHid(),appointmentSearchVO.getSearchParams());
    }

    /**
     * 发送通知邮件
     * @param appointmentSendVO
     * @return
     */
    @Override
    public Boolean send(AppointmentSendVO appointmentSendVO) {

        AppointmentSendBO appointmentSendBO = BeanHelper.copyProperties(appointmentSendVO, AppointmentSendBO.class);
        String string = DateUtil.dateTimeToString(appointmentSendBO.getTime());
        log.info(string);
        String text="亲爱的"+appointmentSendBO.getAccess()+"同学,你好!欢迎你参加"+appointmentSendBO.getCompany()+"校园宣讲会,"+"本次宣讲会的时间是:"+string+",地点:"+appointmentSendBO.getLocaltion()+",届时一定要记得来哦!有非常多的礼品等着大家呢";
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setText(text);
        simpleMailMessage.setSentDate(new Date());
        simpleMailMessage.setSubject(appointmentSendBO.getEmailTitle());
        simpleMailMessage.setFrom(RecommendConstant.SEND_ACCOUNT);
        simpleMailMessage.setTo(appointmentSendBO.getEmail());
        try {
            javaMailSender.send(simpleMailMessage);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 应届生查询预约
     * @return
     */
    @Override
    public List<AppointmentEntity> queryByGid() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        GraduateEntity graduateEntity = graduateEntityMapper.queryAllByGname(userInfo);
        return appointmentEntityMapper.queryByGid(graduateEntity.getGid());
    }

    /**
     * 取消预约
     * @param appointmentCancelVO
     * @return
     */
    @Override
    public Integer cancel(AppointmentCancelVO appointmentCancelVO) {
        AppointmentCancelDTO appointmentCancelDTO = BeanHelper.copyProperties(appointmentCancelVO, AppointmentCancelDTO.class);
        return appointmentEntityMapper.cancel(appointmentCancelDTO);
    }
}
